//
//  MNDRechargeViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/10.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDRechargeViewController.h"
#import "MNDFastPayViewController.h"
#import "MNDNoticeViewController.h"
#import "MNDBaofuBindCardViewController.h"
#import "MNDBaofuBankModel.h"
#import "MNDOpenDeposAlertViewController.h"
#import "MNDDepositWebViewController.h"
#import "MNDDepositAlertViewController.h"
#import "MNDOpenDepositoryViewController.h"

#import "MNDWebViewController.h"

@interface MNDRechargeViewController ()

@property (strong, nonatomic) MNDBaofuBankModel *bankModel;

@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;

@end

@implementation MNDRechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //让footerView占满屏幕底部
    if (kScreenHeight >= 812) {
        //iPhone X
        [self.footerView setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 88 - 10 - 60*2)];
    } else {
        [self.footerView setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 10 - 60*2)];
    }
    
    self.tableView.tableFooterView = self.footerView;
    
    [self loadRechargeNotesInfo];
    
    if (self.isShowCustomBack) {
        [self initBackButtonItem];
    }
}

- (void)initBackButtonItem {
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *itemImage = [UIImage imageNamed:@"navi_back_btn_black"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage: itemImage
                                                             style: UIBarButtonItemStylePlain
                                                            target: self
                                                            action: @selector(backAction:)];
    
    
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = item;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, item];
    }
    for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setTintColor:[UIColor blackColor]];
    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
    
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self loadDepositInfoWithIndexPath:indexPath];
}

- (void)showSetPaymentPasswordAlert {
    __weak MNDOpenDeposAlertViewController *alertVC = [UIStoryboard viewControllerWithIdentifier:[MNDOpenDeposAlertViewController description]];
    
    __weak UIView *alertView = alertVC.view;
    [KeyWindow addSubview:alertView];
    [self.tabBarController addChildViewController:alertVC];
    
    WeakSelf(self);
    
    [alertVC.confirmButton bk_addEventHandler:^(id sender) {
        [alertView removeFromSuperview];
        [alertVC removeFromParentViewController];
        [weakself loadPaymentPassword];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [alertVC.closeButton bk_addEventHandler:^(id sender) {
        [alertView removeFromSuperview];
        [alertVC removeFromParentViewController];
        
    } forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - mark - Network

- (void)loadRechargeNotesInfo {
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_RECHARGE_NOTES_INFO parameters:[MNDSign signedParam] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            NSLog(@"message: %@", responseModel.data);
            
            weakself.messageLabel.text = responseModel.data[@"reminder"];
        }
    } onFailure:^(NSError *error) {
        
    }];
}

- (void)loadDepositInfoWithIndexPath:(NSIndexPath *)indexPath {
    [NetworkProgressHUD showHud];
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_GET_DEPOSIT_INFO parameters:[MNDSign signedParamWithDict:@{@"perfect_type": @"2"}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        [NetworkProgressHUD hideHud];
        if (responseModel.status == ResponseFailure) {
            
            NSString *sign = responseModel.msg[@"sign"];
            if ([sign isEqualToString:kOpenAccount]) {
                [weakself showDepositOpenAlert];
            } else if ([sign isEqualToString:kSetPassword] && indexPath.row == 2) {
                [weakself showSetPaymentPasswordAlert];
            } else {
                [weakself rechargeWithIndexPath:indexPath];
            }
        } else {
            [weakself rechargeWithIndexPath:indexPath];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)rechargeWithIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
//        [self performSegueWithIdentifier:[MNDNoticeViewController description] sender:nil];
        MNDWebViewController *webVC = [[MNDWebViewController alloc] init];
        webVC.webViewUrl = [NSString stringWithFormat:@"%@%@", API_BASE_URL, API_BANK_TRANSFER];
        webVC.loadType = LoadTypeLink;
        webVC.naviBarHidden = NO;
        webVC.title = @"网银转账";

        [self.navigationController pushViewController:webVC animated:YES];
    } else {
        [self performSegueWithIdentifier:[MNDFastPayViewController description] sender:nil];
    }
}

/**
 设置交易密码
 */
- (void)loadPaymentPassword {
    
    [NetworkProgressHUD showHud];
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_DEPOSIT_SET_PASSWORD parameters:[MNDSign signedParamWithDict:@{@"channel": @"app"}] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            MNDDepositWebViewController *webVC = [UIStoryboard viewControllerWithIdentifier:[MNDDepositWebViewController description]];
            webVC.htmlStr = responseModel.data;
            webVC.title = @"设置交易密码";
            webVC.shouldBack = YES;
            NSInteger index = [weakself.navigationController.viewControllers indexOfObject:weakself];
            webVC.fromNaviIndex = index;
            
            [weakself.navigationController pushViewController:webVC animated:YES];
        } else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

//弹出框开通存管
- (void)showDepositOpenAlert {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    __weak MNDDepositAlertViewController *alertVC = [storyboard instantiateViewControllerWithIdentifier:[MNDDepositAlertViewController description]];
    __weak UIView *springView = alertVC.view;
    [KeyWindow addSubview:springView];
    [self.tabBarController addChildViewController:alertVC];
    
    __weak typeof(self) weakSelf = self;
    
    [alertVC.depositBtn bk_addEventHandler:^(id sender) {
        [springView removeFromSuperview];
        [alertVC removeFromParentViewController];
        [weakSelf toOpenDepository];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [alertVC.closeBtn bk_addEventHandler:^(id sender) {
        [springView removeFromSuperview];
        [alertVC removeFromParentViewController];
        
    } forControlEvents:UIControlEventTouchUpInside];
}

/**
 去开通存管页面
 */
- (void)toOpenDepository {
    MNDOpenDepositoryViewController *openViewController = [UIStoryboard viewControllerWithIdentifier:[MNDOpenDepositoryViewController description]];
    openViewController.fromNaviIndex = [self.navigationController.viewControllers indexOfObject:self];
    [self.navigationController pushViewController:openViewController animated:YES];
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    NSInteger index = [self.navigationController.viewControllers indexOfObject:self];
    if ([segue.identifier isEqualToString:[MNDFastPayViewController description]]) {
        MNDFastPayViewController *fastPayVC = segue.destinationViewController;
        fastPayVC.fromIndexVC = index;
        if (self.isFromTender) {
            fastPayVC.isFromTender = self.isFromTender;
        }
    }
    
    if ([segue.identifier isEqualToString:[MNDNoticeViewController description]]) {
        MNDNoticeViewController *notiVC = segue.destinationViewController;
        
        NSString *senderStr = sender;
        if ([senderStr isEqualToString:@"aplipay"]) {
            notiVC.isApliPay = YES;
        } else {
            notiVC.isApliPay = NO;
        }
    }
}


@end
